% Classical Minimum Distance Estimator 
% Headline model
% Model with misreporting and misperception
% Assumption on strategic reporting: 0/10

clear
Version = 5 % 
Model =1

rng(0,"twister")

% Moments:  % HH joint visits +  number of visits x month 
RealMoments =  [ 0.1984849; 0.2250000; 	 0.2279853; 	 0.1637512;  	 59.6792603 ;	58.8961754 ;	 66.8951645 ;	 41.0401993   	  ]
W = [ 0.0001080 ; 0.0001452 ; 0.0001247 ; 0.0001223 ;  5.2188931 ; 4.8619151 ; 5.8740549 ; 2.6794024  ]
MomentCovar     = diag(W);


%Starting parameters
AllStart = [0.12 1 0.06 551 37 0.0002  ; 1 4 1.1 10 10  5  ; 1 4 1.8 10 10  1   ;  0.49 0.14 0.02 717.72 8.64 13.52 ; 32.08 5.0252 93.561 41696 76.414 49.904  ]

n0 = 1;
nz = 5;


for n = n0:nz
  
Repetition      = n

format shortg;
T               = clock ;
Hour            = T(4);
Min             = T(5);
TimeStamp       = [Hour, Min]

StartPar =     AllStart(n:n,:);



lb = [0;0.01;0;0;0;0];
ub = [10000;10000;100000;100000;100000;100000];

nonlcon = @Constraint;
options = optimoptions(@fmincon,'Algorithm','sqp','TolFun', 1e-12, 'MaxIterations',20000, 'MaxFunEvals',20000)
[BetaHat,fval,exitflag,output]     = fmincon(@(param) v5_LossFunction(param, RealMoments, MomentCovar), [StartPar],  [],[],[],[],lb,ub,[],options);


BetaHat;


% Simulated moments
SimMomentst = v5_Moments([BetaHat]);
SimMoments = [SimMomentst, RealMoments]
SimMomentst2 = v5_Moments_all([BetaHat]);


% Store Data
filename1 = sprintf('%s_%d.csv','results/v5_Parameters',n)
filename2 = sprintf('%s_%d.csv','results/v5_Moments_all',n);
W2 = W';
optvar = options.TolFun;
itervar = output.iterations;
BetaHat = BetaHat';
StartPar = StartPar';
DataStore = padcat(BetaHat, SimMomentst, RealMoments,  SimMomentst2, W, StartPar, fval, exitflag, Model,  optvar, itervar); 
csvwrite(filename1,DataStore);
csvwrite(filename2,SimMomentst2);

end

return

